//
//  ZYCLectureRecordHandleExceptionViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/8/23.
//

#import "ZYCLectureRecordHandleExceptionViewController.h"
#import "ZYCTourRecordMarkCell.h"
#import "ZYCAttendanceEvaluateDetailCell.h"
#import "ZYCLectureRecordHandleExceptionBottomView.h"
#import "ZYCLectureRecordHandleExceptionModel.h"
@interface ZYCLectureRecordHandleExceptionViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *comfirmButton;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) ZYCLectureRecordHandleExceptionBottomView *bottomView;
@property (nonatomic, strong) ZYCLectureRecordHandleExceptionModel *model;
@end

@implementation ZYCLectureRecordHandleExceptionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"处理异常";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.comfirmButton];
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView = self.bottomView;
    [self loadDetailData];
}




#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 ) {// 巡堂情况
        ZYCTourRecordMarkCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCTourRecordMarkCell class]) forIndexPath:indexPath];
        cell.titleString = self.dataSource[indexPath.section];
        cell.classStatus =  self.model.classStatus;
        cell.isHid = YES;    
        return cell;
    }else{// 说明
        ZYCAttendanceEvaluateDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCAttendanceEvaluateDetailCell class]) forIndexPath:indexPath];
        cell.titleString =  self.dataSource[indexPath.section];
        cell.contentString = self.model.classContent;
        cell.isHid = YES;
        return cell;
    }
}
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5.0f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}


#pragma mark - getters
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCTourRecordMarkCell class] forCellReuseIdentifier:NSStringFromClass([ZYCTourRecordMarkCell class])];
        [_tableView registerClass:[ZYCAttendanceEvaluateDetailCell class] forCellReuseIdentifier:NSStringFromClass([ZYCAttendanceEvaluateDetailCell class])];
        _tableView.estimatedRowHeight = 50;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        
    }
    return _tableView;
}
- (ZYCLectureRecordHandleExceptionBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[ZYCLectureRecordHandleExceptionBottomView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200.0f)];

    }
    return _bottomView;
}

- (NSArray *)dataSource{
    if (!_dataSource) {
        _dataSource = @[@"巡堂情况:",@"说明:"];
    }
    return _dataSource;
}

- (UIButton *)comfirmButton {
    if (!_comfirmButton) {
        _comfirmButton = [[UIButton alloc] init];
        [_comfirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [_comfirmButton setTitleColor:HEX_RGB(kTitleWhiteColor) forState:UIControlStateNormal];
        _comfirmButton.titleLabel.font = kCustomFont(@"PingFangSC-Regular", 15.0f);
        [_comfirmButton addTarget:self action:@selector(clickRightItem:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _comfirmButton;
}

#pragma mark - private method

- (void)loadDetailData{
    NSDictionary *params = @{
        @"checkId":self.checkId
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_getDetailById requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                self.model = [ZYCLectureRecordHandleExceptionModel yy_modelWithDictionary:responseObject[@"body"]];
                [self.tableView reloadData];
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}

- (void)clickRightItem:(UIButton *)sender{
    if (self.bottomView.exceptContent.length == 0) {
        [MrjToast makeToast:@"请输入异议" makeView:self.view];
        return;
    }
    
    NSDictionary *params = @{
        @"checkId":self.checkId,
        @"exceptContent":self.bottomView.exceptContent
    };
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_updateExceptContent requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            if (SUCCESS(responseObject[@"code"])) {
                [MrjSVProgressHUD showSuccessWithStatus:responseObject[@"body"]];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
